package com.haofengsoft.scce.ui.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshBase;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshListView;
import com.haofengsoft.platform.utils.StringUtil;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.trade.TradeAdapter;
import com.haofengsoft.scce.adapter.trade.TradeModel;

/**
 * 交易
 * 
 * @author zhj
 * 
 */
public class TradeFragment extends BaseFragment {

	private PullToRefreshListView tradePtrl;
	private TradeAdapter tradeAdapter = null;

	private int pageIndex = 0;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.main_trade, container, false);
		view.setOnTouchListener(new DontSpillOnTouchListener());

		tradePtrl = (PullToRefreshListView) view.findViewById(R.id.trade_ptrl);

		tradePtrl.setMode(PullToRefreshBase.Mode.DISABLED);

		ImageView nodataIv = (ImageView) view.findViewById(R.id.trade_no_data_iv);
		nodataIv.setOnClickListener(new DataLoadOnClickListener());
		tradePtrl.setEmptyView(nodataIv);
		tradePtrl.setOnFocusChangeListener(null);
		tradePtrl.setOnHierarchyChangeListener(null);
		tradePtrl.setOnRefreshListener(new LoadPullData());

		initVolleyData();
		return view;
	}

	class DataLoadOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View view) {
			initVolleyData();
		}
	}

	class LoadPullData implements PullToRefreshBase.OnRefreshListener2 {
		// 下拉(手势下拉)
		@Override
		public void onPullDownToRefresh(PullToRefreshBase refreshView) {
			pageIndex = 0;
			tradeAdapter = null;
			initVolleyData();
		}

		@Override
		public void onPullUpToRefresh(PullToRefreshBase refreshView) {
			initVolleyData();
		}
	}

	private void initVolleyData() {
		List<TradeModel> tradeModelList = new ArrayList<TradeModel>();
		for (int i = 0; i < 10; i++) {
			TradeModel tradeModel = new TradeModel();

			tradeModel.setTradeImgUrl(imgUrls[i]);
			tradeModel.setTradeCommodityName(stamps[i]);
			tradeModel.setTradeCommodityPrice("" + new Random().nextInt(99999) + "."
					+ configInt(new Random().nextInt(99)) + "元");

			tradeModel.setTradeSellerName(members[i]);
			tradeModel.setTradeFinishTime("2015-08-28 " + configInt(new Random().nextInt(23)) + ":"
					+ configInt(new Random().nextInt(59)));
			tradeModel.setTradeStatus(1);
			tradeModelList.add(tradeModel);
		}

		refreshData(tradeModelList);
	}

	private String[] members = new String[] { "小民邮缘", "淮安老冯", "北京晨曦", "匿名", "淮南小路", "寂寞百合", "德胜老张", "明", "弥勒佛王",
			"137626胡子" };
	private String[] stamps = new String[] { "红楼梦小型张（好品）", "10克三轮羊银钞", "美品带号炎帝陵小型张", "虎大版(好品)", "漳州木版年画小全张",
			"改革开放三十周年小型张(好品)", "西游记小型张(原包)", "儒林外史风琴折(中品)", "大运河小型张(好品)", "少数民族运动会小全张(原刀)" };

	private String[] imgUrls = new String[] { "/upload/file//20150713/正面-1(4).jpg", "/upload/file//20150710/9-2.jpg",
			"/upload/file//20150713/正面-1.jpg", "/upload/file//20150710/8-1111.jpg",
			"/upload/file//20150713/正面1(2).jpg", "/upload/file//20150713/正面-1(1).jpg",
			"/upload/file//20150713/正面-1(3).jpg", "/upload/file//20150713/正面-1(2).jpg",
			"/upload/file//20150713/正面1.jpg", "/upload/file//20150713/正面-1(6).jpg" };

	private String configInt(int i) {
		String str = String.valueOf(i);
		if (String.valueOf(i).length() == 1) {
			str = "0" + i;
		}
		return str;
	}

	private void refreshData(List<TradeModel> modelList) {
		pageIndex += 1;
		if (null == tradeAdapter) {
			tradeAdapter = new TradeAdapter(getActivity(), modelList);
			tradePtrl.setAdapter(tradeAdapter);
		} else {
			tradeAdapter.addTradeModelList(modelList);
			tradeAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void lazyLoad() {

	}
}
